package com.cl.blog.web.manager;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cl.blog.common.Const;
import com.cl.blog.common.Result;
import com.cl.blog.document.MessageDto;
import com.cl.blog.entity.BNewNotice;
import com.cl.blog.entity.BNotice;
import com.cl.blog.entity.BUser;
import com.cl.blog.service.BNewNoticeService;
import com.cl.blog.service.BNoticeService;
import com.cl.blog.service.BUserService;
import com.cl.blog.util.JwtUtil;
import com.cl.blog.vo.RegisterUserVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author 付
 * @Date 2024-03-23 16:10
 * @Version 1.0
 * @description
 * @since 1.0
 */

@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

	@Resource
	BUserService bUserService;
	@Autowired
	JwtUtil jwtUtil;
	@Resource
	RedisTemplate<String, Object> redisTemplate;
	@Resource
	BNoticeService bNoticeService;
	@Autowired
	BNewNoticeService bNewNoticeService;

	@GetMapping("/list")
	public Result getList(Integer id) {
		return Result.success(bUserService.list(new QueryWrapper<BUser>().eq(id != 0, "id", id)));
	}

	/**
	 * 删除用户
	 *
	 * @param id
	 * @param request
	 * @return
	 */
	@GetMapping("/delete")
	public Result deleteUser(Integer id, HttpServletRequest request) throws Exception {
		boolean b = bUserService.deleteById(id, request);
		if (!b) {
			return Result.error("删除失败");
		}
		return Result.success();
	}


	/**
	 * 封禁用户
	 *
	 * @param id
	 * @param request
	 * @return
	 */
	@GetMapping("/ban")
	@Transactional(rollbackFor = Exception.class)
	public Result banUser(Integer id, HttpServletRequest request) {
		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_ADMIN);

		String adminId = claim.getSubject();
		boolean b = bUserService.banByUserId(id, request);
		if (!b) {
			return Result.error("操作失败");
		}

		MessageDto messageDto = new MessageDto();
		messageDto.setPublisher(Integer.valueOf(adminId));
		messageDto.setTitle("通知");
		messageDto.setData("抱歉，当前账户因违反规定，已被封禁");
		messageDto.setReceiver(id);
		redisTemplate.convertAndSend("limit", messageDto);

		BNewNotice bNotice = new BNewNotice();
		bNotice.setAdminId(Integer.valueOf(adminId));
		bNotice.setTitle("禁用通知");
		bNotice.setContent("抱歉，当前账户因违反规定，已被封禁");
		bNotice.setUserId(id);
		bNotice.setCreateTime(LocalDateTime.now());
		bNotice.setStatus(0);
		bNotice.setType(1);
		boolean save = bNewNoticeService.save(bNotice);

		return Result.success();
	}


	/**
	 * 解除封禁
	 * @param id
	 * @return
	 */
	@GetMapping("/openBan")
	public Result openBan(Integer id,HttpServletRequest request) {
		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_ADMIN);

		String adminId = claim.getSubject();

		BNewNotice bNotice = new BNewNotice();
		bNotice.setAdminId(Integer.valueOf(adminId));
		bNotice.setTitle("解禁通知");
		bNotice.setContent("抱歉，当前账户已解禁");
		bNotice.setUserId(id);
		bNotice.setCreateTime(LocalDateTime.now());
		bNotice.setStatus(0);
		bNotice.setType(1);
		boolean save = bNewNoticeService.save(bNotice);
		return Result.success(bUserService.update(new UpdateWrapper<BUser>().eq("id", id).set("is_delete", 0)));
	}

	@PostMapping("/insert")
	public Result insertUser(@Validated @RequestBody RegisterUserVo registerUser){
		BUser bUser = bUserService.insertUser(registerUser);
		return Result.success(bUser);
	}
}
